package session.stateful;

import entity.AnalyticsEntity;
import entity.ServiceEntity;
import entity.ServicePackageEntity;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateful
@LocalBean
public class ServiceSessionBean {

    @PersistenceContext
    private EntityManager entityManager;

    public void createNewService(ServiceEntity selectedService) {
        entityManager.persist(selectedService);
    }

    public List<ServiceEntity> getServices() {
        Query query = entityManager.createQuery("SELECT s from ServiceEntity s");
        return query.getResultList();
    }

    public void editServiceInfo(ServiceEntity service) {
        entityManager.merge(service);
    }

    public ServiceEntity getServiceById(Long serviceId) {
        return entityManager.find(ServiceEntity.class, serviceId);
    }

    public void createNewServicePackage(ServicePackageEntity selectedServicePackage, ServiceEntity[] selectedServices, AnalyticsEntity[] analytics) {
        entityManager.persist(selectedServicePackage);

        for (int i = 0; i < selectedServices.length; i++) {
            selectedServicePackage.getService().add(selectedServices[i]);
            selectedServices[i].getServicePackage().add(selectedServicePackage);
            entityManager.merge(selectedServices[i]);
        }

        for (int j = 0; j < analytics.length; j++) {
            selectedServicePackage.getAnalytic().add(analytics[j]);
            analytics[j].getServicePackage().add(selectedServicePackage);
            entityManager.merge(analytics[j]);
        }
    }

    public void editServicePackageInfo(ServicePackageEntity selectedServicePackage, ServiceEntity[] selectedServices, AnalyticsEntity[] selectedAnalytics) {
        List<ServiceEntity> serviceList = selectedServicePackage.getService();

        while (!serviceList.isEmpty()) {
            ServiceEntity service = serviceList.get(0);
            serviceList.remove(0);
            service.getServicePackage().remove(selectedServicePackage);
            entityManager.merge(service);
            entityManager.flush();
        }

        for (int i = 0; i < selectedServices.length; i++) {
            selectedServicePackage.getService().add(selectedServices[i]);
            selectedServices[i].getServicePackage().add(selectedServicePackage);
            entityManager.merge(selectedServices[i]);
        }

        List<AnalyticsEntity> analyticList = selectedServicePackage.getAnalytic();

        while (!analyticList.isEmpty()) {
            AnalyticsEntity analytic = analyticList.get(0);
            analyticList.remove(0);
            analytic.getServicePackage().remove(selectedServicePackage);
            entityManager.merge(analytic);
            entityManager.flush();
        }
        
                for (int i = 0; i < selectedAnalytics.length; i++) {
            selectedServicePackage.getAnalytic().add(selectedAnalytics[i]);
            selectedAnalytics[i].getServicePackage().add(selectedServicePackage);
            entityManager.merge(selectedAnalytics[i]);
        }

        entityManager.merge(selectedServicePackage);
    }

    public List<ServicePackageEntity> getServicePackagesForStakeholder() {
        Query query = entityManager.createQuery("SELECT s from ServicePackageEntity s WHERE s.visibility = true");
        return query.getResultList();
    }

    public List<ServicePackageEntity> getServicePackages() {
        Query query = entityManager.createQuery("SELECT s from ServicePackageEntity s");
        return query.getResultList();
    }

    public ServiceEntity[] getServiceByServicePackageId(Long id) {
        ServicePackageEntity servicePackage = entityManager.find(ServicePackageEntity.class, id);
        ServiceEntity[] temp = new ServiceEntity[servicePackage.getService().size()];
        int i = 0;
        for (ServiceEntity service : servicePackage.getService()) {
            temp[i] = service;
            i++;
        }
        return temp;
    }
}
